const lunarCalendarFix = require('lunar-calendar-fix');
const app = require('express')();
const { validationResult, check, matchedData } = require('express-validator');
const reg = /(\d{4})-(\d{2})-(\d{2})/;

/**
 * @summary 黄历假日接口
 * @param {string}  date.query.required
 */
app.get('/', check('date', '请输入正确的日期格式：yyyy-mm-dd').matches(reg), (req, res) => {
  // 验证失败数据
  let err = validationResult(req);
  // 获取匹配的数据
  let allData = matchedData(req);

  if (!err.isEmpty()) {
    res.api_error(err.mapped().date.msg, 1002, err.mapped());
    return;
  }

  reg.test(allData.date);
  const data = lunarCalendarFix.solarToLunar(RegExp.$1, RegExp.$2, RegExp.$3);
  res.api_success(data);
});

module.exports = app;
